import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.SizeExceededException;


/**
 * 2BA7 Project MMS Communications Platform to interact with other phones using MMS messages.
 *
 * @author
 */
public class MMSPlatform {
	
	/** 
	 * Method to send an animal by MMS to another phone
	 * @param address Phone number in URL format e.g. mms://+5550001
	 * @param m The (String)message to send to another phone
	 * @param sendingAlert an Alert to use for status
	 * @param errorAlert an Alert to use for errors
	 * @param appID This application's MMS application ID
	 * @param display Display that is being used
	 */
	
	public static void sendByMMS2(final String address, final String m, final Alert sendingAlert,
			final Alert errorAlert, final String appID, final Display display){
		 MMSMessage message = new MMSMessage();
		 final String encoding = "UTF-8";
		    final String mimeType = "text";
			byte[] contents;
		 
		 
			try {
		
				contents=m.getBytes(encoding);
				message.addPart(new MessagePart(contents, 0, contents.length, mimeType,
				        "id" + 0, "contentLocation", encoding));
			} catch (final SizeExceededException e) {
				e.printStackTrace();
			} catch (final UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		MMSPlatform.promptAndSend(address, message, sendingAlert, errorAlert, display, appID);
	
	}



	/**
	 * Displays security warning and then sends the message.
	 * NB: Separate thread for sending needed to avoid 
	 * contention for display.
	 * 
	 * @param address Phone number to send to
	 * @param message Animal encoded in XML and in MMSMessage instance
	 * @param sendingAlert an Alert to use for status
	 * @param errorAlert an Alert to use for errors
	 * @param display Display that is being used
	 * @param appID This application's MMS application ID
	 */
    private static void promptAndSend(final String address, final MMSMessage message, final Alert sendingAlert,
    		final Alert errorAlert, final Display display, final String appID) {
        try {
        	
            message.setDestination(address);
            final String statusMessage = "Sending message to " + address + "...";
            sendingAlert.setString(statusMessage);
            display.setCurrent(sendingAlert);
            new SenderThread(message, appID).start();
        } catch (final IllegalArgumentException iae) {
            errorAlert.setString(iae.getMessage());
            display.setCurrent(errorAlert);
        }
    }
	
	/** 
	 * Method to receive an animal by MMS from another phone.
	 * Starts a listener and then waits.
	 * 
	 * @param appID This application's MMS application ID
	 * @param display Display that is being used
	 * @param form Form that is being used
	 */
	public static void receiveMMS(final String appID, final Display display, final Form form){
		new ReceiverThread(appID, display, form);
	}
	
	/**
	 * Method to get the message/object received by MMS by thread started in receiveMMS()
	 * 
	 * @return The String(containing the message),
	 * can be modified to return an object received by MMS
	 */
	public static String getFromMMS(){
		String temp;
		 
		temp=("<Start>" + ReceiverThread.getContent() + "</end>");
		return temp;
	}
}
